package com.biqee.service.bq.impl;

import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.biqee.dao.BqQueryDAO;
import com.biqee.dao.BqQueryRelationDAO;
import com.biqee.exception.Assert;
import com.biqee.model.TbQueryModel;
import com.biqee.model.common.PageInfoObject;
import com.biqee.model.db.BqQuery;
import com.biqee.service.bq.BqQueryService;
import com.biqee.service.tb.TbsearchService;

public class BqQueryServiceImpl implements BqQueryService {
	
	private BqQueryDAO bqQueryDAO;
	 
	private BqQueryRelationDAO bqQueryRelationDAO ;
	
	private TbsearchService tbsearchService;
	
	public void save(BqQuery bqQuery){
		Date systemDate = new Date();
		bqQuery.setGmtModified(systemDate);
		bqQuery.setGmtCreate(systemDate);
		bqQueryDAO.insert(bqQuery);
	
	}
	
	
	public void update(BqQuery bqBqQuery ){
		bqBqQuery.setGmtModified(new Date());
		bqQueryDAO.updateByPrimaryKeySelective(bqBqQuery);
	}
	
	
	public void saveOrUpdate(BqQuery bqQuery){
		if(bqQuery.getId()!=null){
			this.update(bqQuery);
		}else{
			this.save(bqQuery);
		}
		
	}
	
	


	public void setBqQueryDAO(BqQueryDAO bqQueryDAO) {
		this.bqQueryDAO = bqQueryDAO;
	}


	@Override
	public List<BqQuery> listByPage(PageInfoObject page) {
		return this.bqQueryDAO.listByPage(page);
	}

	@Override
	public List<BqQuery> listByPage(PageInfoObject page, String tuanId) {
		// TODO Auto-generated method stub
		return this.bqQueryDAO.listByPage(page,tuanId);
	}
	
	
	@Override
	public BqQuery getById(Integer id) {
		return bqQueryDAO.selectByPrimaryKey(id);
	}

	
	@Transactional
	@Override
	public int searchTbItems(TbQueryModel tbQuery) {
		Assert.assertNotNull(tbQuery);
		 this.saveOrUpdate(tbQuery.getBqQuery());
		 int count =  tbsearchService.search(tbQuery); 
		 this.saveOrUpdate(tbQuery.getBqQuery());
		 return count ;
		
	}


	public void setTbsearchService(TbsearchService tbsearchService) {
		this.tbsearchService = tbsearchService;
	}


	@Override
	public int deletedByTuanId(Integer tuanId) {
		
		bqQueryRelationDAO.deleteByTuanId(tuanId);
		return bqQueryDAO.deleteByTuanId(tuanId);
	}


	public void setBqQueryRelationDAO(BqQueryRelationDAO bqQueryRelationDAO) {
		this.bqQueryRelationDAO = bqQueryRelationDAO;
	}

	
	@Transactional
	@Override
	public void delete(Integer id) {
		this.bqQueryRelationDAO.deleteByQueryId(id);
		this.bqQueryDAO.deleteByPrimaryKey(id);
	}



	
	
	
	
	
}
